package cn.szzsi._admin;

import cn.szzsi.service.HtmlService;
import cn.szzsi.common.kit.CommonUtil;
import cn.szzsi.common.model.Article;
import cn.szzsi.common.model.Product;
import cn.szzsi.common.model.SiteConfig;
import com.jfinal.kit.Ret;
import com.jfinal.kit.StrKit;

import java.util.List;

/**
 * 商户模板生成
 *
 */
public class GenerateController extends BaseAdminController<SiteConfig> {
    private String shopId;
    public void index(){
        shopId=getAttr("shopId");
        SiteConfig siteConfig= SiteConfig.getByShopId(shopId);
        if(siteConfig==null || StrKit.isBlank(siteConfig.getShopAlias())){
            renderJson(Ret.fail("info","请先在 我要开店 中填写店铺基本信息!"));
            return;
        }
        HtmlService.service.indexBuildHtml(siteConfig);
        renderJson(Ret.ok("info","生成成功!"));
    }

    public void page(){
        shopId=getAttr("shopId");
        setAttr("shopId",shopId);
        render("page.html");
    }
    public void product(){
        shopId=getAttr("shopId");
        SiteConfig siteConfig= SiteConfig.getByShopId(shopId);
        if(siteConfig==null){
            renderHtml("<script>alert('请先在 我要开店 中填写店铺基本信息!');history.go(-1);</script>");
            return;
        }
        String shopAlias=siteConfig.getStr("shopAlias");
        boolean isMobileDevice = CommonUtil.isMobileDevice(getRequest());
        String path= CommonUtil.getPathByUidAndType(shopId,isMobileDevice);
        List<Product> list = Product.dao.getByShopId(shopId,"createDate","desc");
        for(Product p:list){
            HtmlService.service.businessProductContentBuildHtml(p,path,shopAlias,shopId);
        }
        renderJson(Ret.ok());
    }
    public void article(){
        shopId=getAttr("shopId");
        //获取店铺别名
        SiteConfig siteConfig= SiteConfig.getByShopId(shopId);
        if(siteConfig==null){
            renderHtml("<script>alert('请先在 我要开店 中填写店铺基本信息!');history.go(-1);</script>");
            return;
        }
        String shopAlias=siteConfig.getStr("shopAlias");
        List<Article> list = Article.dao.find("select * from mall_article where uid = ?",shopId);
        boolean isMobileDevice = CommonUtil.isMobileDevice(getRequest());
        String path= CommonUtil.getPathByUidAndType(shopId,isMobileDevice);
        for(Article r:list){
            HtmlService.service.businessArticleContentBuildHtml(r,isMobileDevice,path,shopId,shopAlias);
        }
        renderJson(Ret.ok());
    }


}

















